﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Data;
using TTBusiness.DB_Linq;
namespace TTBusiness.ClassDB
{
    /// <summary>
    /// Create by ThiepND 23/3/2011
    /// </summary>
    public class clsBSOrder
    {
        TTMarketing_DBDataContext objDB = new TTMarketing_DBDataContext();
        public List<tblOrder> getAll()
        {
            try
            {
                var list = from a in objDB.tblOrders
                           orderby a.ID descending
                           select a;
                return list.ToList<tblOrder>();
            }
            catch (Exception)
            {

                throw;
            }
        }
        public List<tblOrder> getByAccount(int accountId)
        {
            try
            {
                var list = from a in objDB.tblOrders
                           where a.AccountID.Equals(accountId)
                           orderby a.ID descending
                           select a;
                return list.ToList<tblOrder>();
            }
            catch (Exception)
            {

                throw;
            }
        }
        
        public tblOrder getById(int id)
        {
            try
            {
                var obj = objDB.tblOrders.SingleOrDefault(a => a.ID == id);
                return obj;
            }
            catch (Exception)
            {

                throw;
            }
        }
        
        public void insert(tblOrder obj)
        {
            try
            {
                obj.Date = DateTime.Now;
                objDB.tblOrders.InsertOnSubmit(obj);
                objDB.SubmitChanges();
            }
            catch (Exception)
            {

                throw;
            }
        }
        public int insert2(tblOrder obj)
        {
            try
            {
                obj.Date = DateTime.Now;
                objDB.tblOrders.InsertOnSubmit(obj);
                objDB.SubmitChanges();
                var i = (from o in objDB.tblOrders
                         select o.ID).Max();
                return i;
            }
            catch (Exception)
            {

                throw;
            }
        }

        public void update(tblOrder obj)
        {
            try
            {
                var objj = objDB.tblOrders.SingleOrDefault(a => a.ID == obj.ID);
                if (objj != null)
                {
                    obj.Date = DateTime.Now;
                    objj.Detail = obj.Detail;
                    objj.ID = obj.ID;
                    objj.AccountID = obj.AccountID;
                    objj.Address = obj.Address;
                    objj.Date = obj.Date;
                    objj.Detail = obj.Detail;
                    objj.Email = obj.Email;
                    objj.FullName = obj.FullName;
                    obj.Phone = objj.Phone;
                    objDB.SubmitChanges();
                }
            }
            catch (Exception)
            {

                throw;
            }
        }
        //public void update(int id, bool status)
        //{
        //    try
        //    {
        //        var obj = objDB.tblOrders.SingleOrDefault(a => a.ID == id);
        //        if (obj != null)
        //        {
        //            obj.Status = status;
        //            objDB.SubmitChanges();
        //        }
        //    }
        //    catch (Exception)
        //    {

        //        throw;
        //    }
        //}
        public void delete(int id)
        {
            try
            {
                var obj = (from a in objDB.tblOrders
                           where a.ID == id
                           select a).SingleOrDefault();
                objDB.tblOrders.DeleteOnSubmit(obj);
                objDB.SubmitChanges();
            }
            catch (Exception)
            {

                throw;
            }
        }
    }
}
